﻿using System;
using System.Security.Cryptography;
using System.Text;

namespace CLFramework.Security.Cryptography
{
    public class EncryptHelper
    {
        public static string DecryptString(string strText, string key)
        {
            try
            {
                byte[] buffer = new MD5CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes(key));
                TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();
                provider.Key = buffer;
                provider.Mode = CipherMode.ECB;
                byte[] inputBuffer = Convert.FromBase64String(strText);
                return Encoding.ASCII.GetString(provider.CreateDecryptor().TransformFinalBlock(inputBuffer, 0, inputBuffer.Length));
            }
            catch (Exception ex)
            {
                throw new ApplicationException("解密字符串错误：" + key, ex);
            }
        }

        public static string DecryptUTF8String(string strText, string key)
        {
            try
            {
                byte[] buffer = new MD5CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(key));
                TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();
                provider.Key = buffer;
                provider.Mode = CipherMode.ECB;
                byte[] inputBuffer = Convert.FromBase64String(strText);
                return Encoding.UTF8.GetString(provider.CreateDecryptor().TransformFinalBlock(inputBuffer, 0, inputBuffer.Length));
            }
            catch (Exception ex)
            {
                throw new ApplicationException("解密字符串错误：" + key, ex);
            }
        }

        public static string EncryptString(string strText, string key)
        {
            try
            {
                byte[] buffer = new MD5CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes(key));
                TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();
                provider.Key = buffer;
                provider.Mode = CipherMode.ECB;
                byte[] bytes = Encoding.ASCII.GetBytes(strText);
                string str = Convert.ToBase64String(provider.CreateEncryptor().TransformFinalBlock(bytes, 0, bytes.Length));
                provider = null;
                return str;
            }
            catch (Exception ex)
            {
                throw new ApplicationException("加密字符串错误：" + key, ex);
            }
        }

        public static string EncryptUTF8String(string strText, string key)
        {
            try
            {
                byte[] buffer = new MD5CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(key));
                TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();
                provider.Key = buffer;
                provider.Mode = CipherMode.ECB;
                byte[] bytes = Encoding.UTF8.GetBytes(strText);
                string str = Convert.ToBase64String(provider.CreateEncryptor().TransformFinalBlock(bytes, 0, bytes.Length));
                provider = null;
                return str;
            }
            catch (Exception ex)
            {
                throw new ApplicationException("加密字符串错误：" + key, ex);
            }
        }

        /// <summary>
        /// 对字符串进行SHA1哈希
        /// </summary>
        /// <param name="strPassword"></param>
        /// <returns></returns>
        public static byte[] GetPassword(string strPassword)
        {
            UnicodeEncoding UE = new UnicodeEncoding();
            byte[] arSource = UE.GetBytes(strPassword);

            SHA1 SHharsh = new SHA1Managed();
            byte[] arHarshValue = SHharsh.ComputeHash(arSource);

            return arHarshValue;
        }
    }
}
